#include "Metgine/UI/Button.hpp"
#include "Metgine/Input/MouseManager.hpp"
#include "Metgine/Core/Engine.hpp"
#include "Metgine/Core/Renderer.hpp"

#include <iostream>
using namespace Metgine::Input;
using namespace Metgine::Core;

namespace Metgine
{
namespace UI
{

Button::Button(const std::string& name) :
    Widget(name)
{
}

void Button::SetButtonSize(int width, int height)
{
    m_buttonSize = sf::Vector2i(width, height);
    ((sf::Sprite&)m_sprite.GetData()).SetSubRect(sf::IntRect(0, 0, width, height));
}

void Button::OnClick()
{
    ((sf::Sprite&)m_sprite.GetData()).SetSubRect(sf::IntRect(Down * m_buttonSize.x, 0, Down * (m_buttonSize.x + m_buttonSize.x / 2) + 1, m_buttonSize.y));
    m_state = Down;

    Widget::OnClick();
}

void Button::OnMouseEnter()
{
    ((sf::Sprite&)m_sprite.GetData()).SetSubRect(sf::IntRect(Hover * m_buttonSize.x, 0, Hover * (m_buttonSize.x + m_buttonSize.x), m_buttonSize.y));
    m_state = Hover;

    Widget::OnMouseEnter();
}

void Button::OnMouseLeave()
{
    ((sf::Sprite&)m_sprite.GetData()).SetSubRect(sf::IntRect(0, 0, m_buttonSize.x, m_buttonSize.y));
    m_state = Up;

    Widget::OnMouseLeave();
}


void Button::Render()
{
    Engine::Get()->GetRenderer()->Render(m_sprite);
}

}; // UI
}; // Metgine

